/*
 * Created by Jaren at 2021/12/29 13:34
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/ezcan/bean/settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class EucSettings extends AbsSettings {
  Uint16 speedLimit = Uint16.zero; // 限速

  Int16 pitchAngleZero = Int16.zero; // 前后零点角度

  Uint8 volume = Uint8.zero; // 音量
  Uint4 spinKillState = Uint4.zero; // 防飞转按键开关状态

  Uint4 headlightState = Uint4.zero; // 前照明灯开关

  Uint4 lockState = Uint4.zero; // 锁车状态

  Uint4 soundPackState = Uint4.zero; // 音效开关

  /// 当前音效包ID
  Uint32 soundPackId = Uint32.zero;

  // private
  EucSettings();

  EucSettings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes, [int offset = 0]) {
    speedLimit = ByteUtil.toUint16(ByteUtil.bytes2IntLe(slowInfoRspMsgExtraBytes, 60, 4) ~/ 10);
    pitchAngleZero = ByteUtil.toInt16((ByteUtil.bytes2IntLe(slowInfoRspMsgExtraBytes, 56, 4) ~/ 655.36));
    headlightState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 80, 4));
    soundPackState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 84, 4));
    volume = ByteUtil.toUint8(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 125, 4) ~/ 100);
    spinKillState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 129, 1));
  }

  @override
  int length() {
    return 0;
  }

  @override
  String toString() {
    return 'EucSettings{${super.toString()}, speedLimit: $speedLimit, pitchAngleZero: $pitchAngleZero, volume: $volume, spinKillState: $spinKillState, headlightState: $headlightState, lockState: $lockState, soundPackState: $soundPackState, soundPackId: $soundPackId}';
  }
}

class V5Settings extends EucSettings {
  V5Settings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes, [int offset = 0]) {
    speedLimit = ByteUtil.toUint16(ByteUtil.bytes2IntLe(slowInfoRspMsgExtraBytes, 60, 4) ~/ 10);
    pitchAngleZero = ByteUtil.toInt16((ByteUtil.bytes2IntLe(slowInfoRspMsgExtraBytes, 56, 4) ~/ 655.36));
    headlightState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 80, 4));
    soundPackState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 84, 4));
  }
}

class V8Settings extends EucSettings {
  Uint4 driveMode = Uint4.zero; // 驱动模式

  Uint8 pedalSensitivity = Uint8.zero; // 踏板灵敏度
  Uint4 transportationModeState = Uint4.zero; // 运输模式开关

  Uint4 lightEffectState = Uint4.zero; // 氛围灯开关状态
  Uint32 lightEffectId = Uint32.zero; // 氛围灯id

  V8Settings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes, [int offset = 0]) : super.fromBytes(slowInfoRspMsgExtraBytes, offset) {
    if (slowInfoRspMsgExtraBytes.length > 124) pedalSensitivity = ByteUtil.toUint8((ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 124) * 32 - 2048) * 100 ~/ 2048);
    if (slowInfoRspMsgExtraBytes.length > 130) lightEffectState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 130));
    if (slowInfoRspMsgExtraBytes.length > 132) driveMode = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 132) & 0x01);
  }
}

class V10Settings extends EucSettings {
  Uint4 driveMode = Uint4.zero; // 驱动模式

  Uint8 pedalSensitivity = Uint8.zero; // 踏板灵敏度
  Uint4 transportationModeState = Uint4.zero; // 运输模式开关
  Uint16 standByTime = Uint16.zero; // 待机时间，单位s

  Uint4 lightEffectState = Uint4.zero; // 氛围灯开关状态
  Uint32 lightEffectId = Uint32.zero; // 氛围灯id

  V10Settings.fromBytes(Uint8ClampedList slowInfoRspMsgExtraBytes, [int offset = 0]) : super.fromBytes(slowInfoRspMsgExtraBytes, offset) {
    if (slowInfoRspMsgExtraBytes.length > 124) pedalSensitivity = ByteUtil.toUint8((ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 124) * 32 - 2048) * 100 ~/ 2048);
    if (slowInfoRspMsgExtraBytes.length > 130) lightEffectState = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 130, 1));
    if (slowInfoRspMsgExtraBytes.length > 131) standByTime = ByteUtil.toUint16(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 131, 1) * 60);
    if (slowInfoRspMsgExtraBytes.length > 132) driveMode = ByteUtil.toUint4(ByteUtil.bytes2UIntLe(slowInfoRspMsgExtraBytes, 132) & 0x01);
  }

  @override
  String toString() {
    return 'V10Settings{${super.toString()}, driveMode: $driveMode, pedalSensitivity: $pedalSensitivity, transportationModeState: $transportationModeState, standByTime: $standByTime, lightEffectState: $lightEffectState, lightEffectId: $lightEffectId}';
  }
}
